package cn.java71.servlet;

import cn.java71.entity.Page;
import cn.java71.entity.Student;
import cn.java71.service.StudentService;
import cn.java71.util.StrUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebInitParam;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Date;

@WebServlet(name = "StudentPagingServlet",value = "/student")
@WebInitParam(name = "charset",value = "gbk")
public class StudentPagingServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doGet(request,response);
    }
    StudentService studentService=new StudentService();
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//        if(request.getSession().getAttribute("USER_LOGIN")==null)
//        {
//            response.sendRedirect("/index.jsp");
//            return;
//        }

        request.setCharacterEncoding("utf-8");
        int p=1;
        try {
            p = Integer.parseInt(request.getParameter("p"));
        }catch (Exception e){}
        int pageSize=20;
        String sname=request.getParameter("sname");
        sname=StrUtil.toStr(sname);
        String qq= StrUtil.toStr(request.getParameter("qq"));
        String telephone=StrUtil.toStr(request.getParameter("telephone"));
        String startdate=StrUtil.toStr(request.getParameter("startdate"));
        String enddate=StrUtil.toStr(request.getParameter("enddate"));
        Date start=StrUtil.toDate(startdate);
        Date end=StrUtil.toDate(enddate);

        Page<Student> students = studentService.search(p, pageSize, sname, start, end);
        //>>>>
        //数据怎么带过去
        request.setAttribute("p",p);
        request.setAttribute("sname",sname);

        request.setAttribute("qq",qq);
        request.setAttribute("telephone",telephone);
        request.setAttribute("startdate",startdate);
        request.setAttribute("enddate",enddate);
        request.setAttribute("students",students);
        //jsp 怎么去？
        request.getRequestDispatcher("/WEB-INF/views/students2.jsp").forward(request,response);
    }
    String set;
    @Override
    public void init() throws ServletException {
        // super.getInitParameter("charset")
        set= super.getServletConfig().getInitParameter("charset");
    }
}
